<?php

namespace frontend\modules\resource\models;

use frontend\modules\common\models\BaseParam;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Banner;

/**
 * BannerSearch represents the model behind the search form of `common\models\Banner`.
 */
class BannerSearch extends Banner
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'ba_sort', 'status', 'type'], 'integer'],
            [['ba_name', 'ba_url_pc', 'ba_url_mini', 'br_id', 'img_url', 'created_at', 'updated_at', 'deleted_at'], 'safe'],
        ];
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params, $branchId)
    {
        $query = Banner::find();

        // add conditions that should always apply here
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => BaseParam::$page,
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'ba_sort' => $this->ba_sort,
            'status' => $this->status,
            'type' => $this->type,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'deleted_at' => $this->deleted_at,
        ]);

        $query->andFilterWhere(['like', 'ba_name', $this->ba_name])
            ->andFilterWhere(['like', 'ba_url_pc', $this->ba_url_pc])
            ->andFilterWhere(['like', 'ba_url_mini', $this->ba_url_mini])
            ->andFilterWhere(['like', 'br_id', $branchId])
            ->andFilterWhere(['like', 'img_url', $this->img_url]);

        return $dataProvider;
    }
}
